package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_org;

import cn.com.api.service.ApiU_orgService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_org/")
public class ApiU_orgController {
	@Autowired
	private ApiU_orgService apiU_orgService;

	private static Logger logger = Logger.getLogger(ApiU_orgController.class);

	/**
	 * 新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertU_org")
	public void insertU_org(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		String corp_code = request.getParameter("corp_code");
		String cur_index = request.getParameter("cur_index");
		String corp_fname = request.getParameter("corp_fname");
		String corp_sname = request.getParameter("corp_sname");
		String belong = request.getParameter("belong");
		String address = request.getParameter("address");
		String corp_size = request.getParameter("corp_size");
		String url = request.getParameter("url");
		String logo = request.getParameter("logo");
		String description = request.getParameter("description");
		String back_img = request.getParameter("back_img");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			/*
			 * if(c_uid!=null && !"".equals(c_uid.trim())){ ApiU_org u_org = new
			 * ApiU_org();
			 * 
			 * u_org.setId(id); u_org.setCorp_code(corp_code);
			 * u_org.setCur_index(cur_index); u_org.setCorp_fname(corp_fname);
			 * u_org.setCorp_sname(corp_sname); u_org.setBelong(belong);
			 * u_org.setAddress(address); u_org.setCorp_size(corp_size);
			 * u_org.setUrl(url); u_org.setLogo(logo);
			 * u_org.setDescription(description); u_org.setBack_img(back_img);
			 * u_org.setExt1(ext1); u_org.setExt2(ext2); u_org.setExt3(ext3);
			 * u_org.setExt4(ext4); u_org.setExt5(ext5);
			 * 
			 * apiU_orgService.insertU_org(u_org); // return
			 * map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			 * map.put(Utils.MESSAGE, "添加成功！"); }
			 */
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_orgById")
	public void findU_orgById(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			ApiU_org u_org = apiU_orgService.findU_orgById(Integer.valueOf(id));

			// return
			map.put("datas", u_org);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_orgs")
	public void findU_orgs(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<ApiU_org> u_orgs = apiU_orgService.findU_orgs();

			// return
			map.put("datas", u_orgs);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateU_org")
	public void updateU_org(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		String corp_code = request.getParameter("corp_code");
		String cur_index = request.getParameter("cur_index");
		String corp_fname = request.getParameter("corp_fname");
		String corp_sname = request.getParameter("corp_sname");
		String belong = request.getParameter("belong");
		String address = request.getParameter("address");
		String corp_size = request.getParameter("corp_size");
		String url = request.getParameter("url");
		String logo = request.getParameter("logo");
		String description = request.getParameter("description");
		String back_img = request.getParameter("back_img");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");

		// String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {
				ApiU_org u_org = new ApiU_org();
				// 获取原数据
				u_org = apiU_orgService.findU_orgById(Integer.valueOf(id));
				// 修改
				// u_org.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiU_orgService.updateU_org(u_org);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteU_org")
	public void deleteU_org(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {

				Integer ret_I = apiU_orgService.deleteU_org(Integer.parseInt(id));
				if (ret_I > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "检查失败！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
